# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

import pymysql
from scrapy.utils.project import get_project_settings

# class MaoyanWeek13Pipeline:
#     def process_item(self, item, spider):
#         return item


class MysqlPipeline(object):
    def __init__(self):
        settings = get_project_settings()
        self.connect = pymysql.connect(
            host=settings['MYSQL_HOST'],
            db=settings['MYSQL_DBNAME'],
            user=settings['MYSQL_USER'],
            password=settings['MYSQL_PASSWORD'],
            port = settings['MYSQL_PORT'],
            # charset = 'utf8',
            cursorclass = pymysql.cursors.DictCursor,
            # use_unicode = True
        )
        self.cursor = self.connect.cursor()

    def process_item(self, item, spider):
        sql = "insert into maoyan(m_title, m_type, m_time) values (%s,%s,%s)"
        params = (item['movie_title'],item['movie_type'],item['movie_time'])
        self.cursor.execute(sql,params)
        self.connect.commit()
        return item

    def close_spider(self, spider):
        self.cursor.close()
        self.connect.close()